<?php
/**
 * Created by PhpStorm.
 * User: xiong
 * Date: 2018/2/6
 * Time: 17:47
 */

namespace App\Http\Controllers\Api;


use App\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Resources\UserResource;
use App\Models\CashLog;
use App\Models\Fund;
use App\Models\Gift;
use App\Models\MoneyLog;
use App\Models\PointLog;
use App\Models\Stock;
use Carbon\Carbon;
use Encore\Admin\Grid;
use Illuminate\Http\Request;

class ProfileController extends Controller
{
    use ApiResponse;

    public function user(Request $request)
    {
        $user = $request->user('api');
        return response()->json(['msg' => '获取成功', 'data' => ['user' => new UserResource($user)]], 200);
    }

    public function update(Request $request)
    {
        $user = $request->user('api');
        if ($user->level == 1) {
            $user->level = 2;
            $user->save();
            return $this->message('升级成功');
        } else {
            return $this->message('升级失败', [], 400);

        }
    }

    public function config(Request $request)
    {

//        dd();
//        $pay_date = Carbon::createFromDate(date('Y'), date('m'), config('pay_month_day'))->hour(0)->minute(0)->second(0);
//        $pay_date->addMonth(1);
//        dd($pay_date);
        return $this->message("获取成功", ["stock" => config('stock_value')]);
    }

    public function cash_log(Request $request)
    {
        $user = $request->user('api');
        $logs = CashLog::whereUserId($user->id)->paginate();
        return $this->message('获取成功', $logs->items());
    }


    public function cash(Request $request)
    {
        $user = $request->user('api');
        $money = $request->input('money', 0);
        if ($money > $user->money) {
            return $this->message('申请失败,金额不足', [], 400);
        } else {
            $user->money = $user->money - $money;
            $user->save();
            $insert = [];
            $insert['money'] = $money;
            $insert['user_id'] = $user->id;
            $insert['order_no'] = "C" . date('YmdHis') . random_int(1000, 9999);
            $insert['status'] = 0;
            CashLog::create($insert);
            return $this->message('申请成功');
        }

    }
}